package com.fire.eyes.service.impl;

import com.fire.eyes.constants.ErrorCodeConstants;
import com.fire.eyes.dao.TMitChannelMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.fire.core.service.ITMitErrorCodeService;
import com.fire.core.util.AjaxResult;
import com.fire.eyes.model.TMitChannel;
import com.fire.eyes.request.QueryChannelReq;
import com.fire.eyes.response.element.QueryChannelResp;
import com.fire.eyes.service.ITMitChannelService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 渠道表 服务实现类
 * </p>
 *
 * @author raoyuxing123
 * @since 2018-04-28
 */
@Service
public class TMitChannelServiceImpl extends ServiceImpl<TMitChannelMapper, TMitChannel> implements ITMitChannelService {
	 private static Logger logger = Logger.getLogger(TMitChannelServiceImpl.class);
	@Autowired
	private ITMitErrorCodeService itMitErrorCodeService; 
	@Autowired
	private TMitChannelMapper channelMapper;
	
	/**
	 * 渠道查询*
	 */
	@Override
	public AjaxResult queryChannel(String json) {
		QueryChannelReq queryChannelReq = JSON.parseObject(json, QueryChannelReq.class);
		AjaxResult ajaxResult=new AjaxResult();
		if(queryChannelReq==null){
			logger.info("入参不能空");
			ajaxResult.setContext(null);
			ajaxResult.setMessage("入参不能空");
			ajaxResult.setStatus(ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL);
			String tips=itMitErrorCodeService.getTips(ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL+"]"+"入参不能空");
			}
			return ajaxResult;
		}
		String channelmark=queryChannelReq.getChannelmark();
		if(StringUtils.isEmpty(channelmark)){
			logger.info("channelmark入参不能空");
			ajaxResult.setContext(null);
			ajaxResult.setMessage("channelmark不可为空");
			ajaxResult.setStatus(ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL);
			String tips=itMitErrorCodeService.getTips(ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.SYS_CHANNELPARAM_ISNULL+"]"+"channelmark不可为空");
			}
			return ajaxResult;
		}
		TMitChannel channel=new TMitChannel();
		QueryChannelResp queryChannelResp=new QueryChannelResp();
		channel.setChannelmark(channelmark);
		try {
			channel=channelMapper.selectOne(channel);
		} catch (Exception e) {
			logger.error("数据库异常",e);
			ajaxResult.setContext(null);
			ajaxResult.setMessage("数据库异常");
			ajaxResult.setStatus(ErrorCodeConstants.SYS_DATAERROR);
			String tips=itMitErrorCodeService.getTips(ErrorCodeConstants.SYS_DATAERROR);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.SYS_DATAERROR+"]"+"数据库异常");
			}
			return ajaxResult;
		}
		if(channel!=null){
			queryChannelResp.setChanelstatus(channel.getChannelmark());
			queryChannelResp.setChannelid(channel.getChannelid().toString());
			queryChannelResp.setChannelname(channel.getChannelname());
			queryChannelResp.setChanneltpye(channel.getChanneltpye().toString());
			ajaxResult.setContext(queryChannelResp);
			ajaxResult.setMessage("成功");
			ajaxResult.setStatus(ErrorCodeConstants.SUCCESS);
			String tips=itMitErrorCodeService.getTips(ErrorCodeConstants.SUCCESS);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.SUCCESS+"]"+"成功");
			}
			return ajaxResult;

		}else{
			logger.info("没有这条信息");
			ajaxResult.setContext(null);
			ajaxResult.setMessage("没有这条信息");
			ajaxResult.setStatus(ErrorCodeConstants.SYS_CHANNELMARK_ISNULL);
			String tips=itMitErrorCodeService.getTips(ErrorCodeConstants.SYS_CHANNELMARK_ISNULL);
			if(StringUtils.isNotBlank(tips)){
				ajaxResult.setTips(tips);
			}else{
				ajaxResult.setTips("["+ErrorCodeConstants.SYS_CHANNELMARK_ISNULL+"]"+"没有这条信息");
			}
			return ajaxResult;

		}
			

	}


}
